home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / gnumake.zip / VARIABLE.C < prev    next >
C/C++ Source or Header  |  1994-06-07  |  22KB  |  834 lines

  1. /* Internals of variables for GNU Make.
  2. Copyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "variable.h"
  22. #include "dep.h"
  23. #include "file.h"
  24.  
  25. /* Hash table of all global variable definitions.  */
  26.  
  27. #ifndef    VARIABLE_BUCKETS
  28. #define VARIABLE_BUCKETS        523
  29. #endif
  30. #ifndef    PERFILE_VARIABLE_BUCKETS
  31. #define    PERFILE_VARIABLE_BUCKETS    23
  32. #endif
  33. #ifndef    SMALL_SCOPE_VARIABLE_BUCKETS
  34. #define    SMALL_SCOPE_VARIABLE_BUCKETS    13
  35. #endif
  36. static struct variable *variable_table[VARIABLE_BUCKETS];
  37. static struct variable_set global_variable_set
  38.   = { variable_table, VARIABLE_BUCKETS };
  39. static struct variable_set_list global_setlist
  40.   = { 0, &global_variable_set };
  41. struct variable_set_list *current_variable_set_list = &global_setlist;
  42.  
  43. /* Implement variables.  */
  44.  
  45. /* Define variable named NAME with value VALUE in SET.  VALUE is copied.
  46.    LENGTH is the length of NAME, which does not need to be null-terminated.
  47.    ORIGIN specifies the origin of the variable (makefile, command line
  48.    or environment).
  49.    If RECURSIVE is nonzero a flag is set in the variable saying
  50.    that it should be recursively re-expanded.  */
  51.  
  52. static struct variable *
  53. define_variable_in_set (name, length, value, origin, recursive, set)
  54.      char *name;
  55.      unsigned int length;
  56.      char *value;
  57.      enum variable_origin origin;
  58.      int recursive;
  59.      struct variable_set *set;
  60. {
  61.   register unsigned int i;
  62.   register unsigned int hashval;
  63.   register struct variable *v;
  64.  
  65.   hashval = 0;
  66.   for (i = 0; i < length; ++i)
  67.     HASH (hashval, name[i]);
  68.   hashval %= set->buckets;
  69.  
  70.   for (v = set->table[hashval]; v != 0; v = v->next)
  71.     if (*v->name == *name
  72.     && !strncmp (v->name + 1, name + 1, length - 1)
  73.     && v->name[length] == '\0')
  74.       break;
  75.  
  76.   if (env_overrides && origin == o_env)
  77.     origin = o_env_override;
  78.  
  79.   if (v != 0)
  80.     {
  81.       if (env_overrides && v->origin == o_env)
  82.     /* V came from in the environment.  Since it was defined
  83.        before the switches were parsed, it wasn't affected by -e.  */
  84.     v->origin = o_env_override;
  85.  
  86.       /* A variable of this name is already defined.
  87.      If the old definition is from a stronger source
  88.      than this one, don't redefine it.  */
  89.       if ((int) origin >= (int) v->origin)
  90.     {
  91.       if (v->value != 0)
  92.         free (v->value);
  93.       v->value = savestring (value, strlen (value));
  94.       v->origin = origin;
  95.       v->recursive = recursive;
  96.     }
  97.       return v;
  98.     }
  99.  
  100.   /* Create a new variable definition and add it to the hash table.  */
  101.  
  102.   v = (struct variable *) xmalloc (sizeof (struct variable));
  103.   v->name = savestring (name, length);
  104.   v->value = savestring (value, strlen (value));
  105.   v->origin = origin;
  106.   v->recursive = recursive;
  107.   v->expanding = 0;
  108.   v->export = v_default;
  109.   v->next = set->table[hashval];
  110.   set->table[hashval] = v;
  111.   return v;
  112. }
  113.  
  114. /* Define a variable in the current variable set.  */
  115.  
  116. struct variable *
  117. define_variable (name, length, value, origin, recursive)
  118.      char *name;
  119.      unsigned int length;
  120.      char *value;
  121.      enum variable_origin origin;
  122.      int recursive;
  123. {
  124.   return define_variable_in_set (name, length, value, origin, recursive,
  125.                  current_variable_set_list->set);
  126. }
  127.  
  128. /* Define a variable in FILE's variable set.  */
  129.  
  130. struct variable *
  131. define_variable_for_file (name, length, value, origin, recursive, file)
  132.      char *name;
  133.      unsigned int length;
  134.      char *value;
  135.      enum variable_origin origin;
  136.      int recursive;
  137.      struct file *file;
  138. {
  139.   return define_variable_in_set (name, length, value, origin, recursive,
  140.                  file->variables->set);
  141. }
  142.  
  143. /* Lookup a variable whose name is a string starting at NAME
  144.    and with LENGTH chars.  NAME need not be null-terminated.
  145.    Returns address of the `struct variable' containing all info
  146.    on the variable, or nil if no such variable is defined.  */
  147.  
  148. struct variable *
  149. lookup_variable (name, length)
  150.      char *name;
  151.      unsigned int length;
  152. {
  153.   register struct variable_set_list *setlist;
  154.  
  155.   register unsigned int i;
  156.   register unsigned int rawhash = 0;
  157.  
  158.   for (i = 0; i < length; ++i)
  159.     HASH (rawhash, name[i]);
  160.  
  161.   for (setlist = current_variable_set_list;
  162.        setlist != 0; setlist = setlist->next)
  163.     {
  164.       register struct variable_set *set = setlist->set;
  165.       register unsigned int hashval = rawhash % set->buckets;
  166.       register struct variable *v;
  167.  
  168.       for (v = set->table[hashval]; v != 0; v = v->next)
  169.     if (*v->name == *name
  170.         && !strncmp (v->name + 1, name + 1, length - 1)
  171.         && v->name[length] == 0)
  172.       return v;
  173.     }
  174.  
  175.   return 0;
  176. }
  177.  
  178. /* Initialize FILE's variable set list.  If FILE already has a variable set
  179.    list, the topmost variable set is left intact, but the the rest of the
  180.    chain is replaced with FILE->parent's setlist.  */
  181.  
  182. void
  183. initialize_file_variables (file)
  184.      struct file *file;
  185. {
  186.   register struct variable_set_list *l = file->variables;
  187.   if (l == 0)
  188.     {
  189.       l = (struct variable_set_list *)
  190.     xmalloc (sizeof (struct variable_set_list));
  191.       l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
  192.       l->set->buckets = PERFILE_VARIABLE_BUCKETS;
  193.       l->set->table = (struct variable **)
  194.     xmalloc (l->set->buckets * sizeof (struct variable *));
  195.       bzero ((char *) l->set->table,
  196.          l->set->buckets * sizeof (struct variable *));
  197.       file->variables = l;
  198.     }
  199.  
  200.   if (file->parent == 0)
  201.     l->next = &global_setlist;
  202.   else
  203.     {
  204.       if (file->parent->variables == 0)
  205.     initialize_file_variables (file->parent);
  206.       l->next = file->parent->variables;
  207.     }
  208. }
  209.  
  210. /* Pop the top set off the current variable set list,
  211.    and free all its storage.  */
  212.  
  213. void
  214. pop_variable_scope ()
  215. {
  216.   register struct variable_set_list *setlist = current_variable_set_list;
  217.   register struct variable_set *set = setlist->set;
  218.   register unsigned int i;
  219.  
  220.   current_variable_set_list = setlist->next;
  221.   free ((char *) setlist);
  222.  
  223.   for (i = 0; i < set->buckets; ++i)
  224.     {
  225.       register struct variable *next = set->table[i];
  226.       while (next != 0)
  227.     {
  228.       register struct variable *v = next;
  229.       next = v->next;
  230.  
  231.       free (v->name);
  232.       free ((char *) v);
  233.     }
  234.     }
  235.   free ((char *) set->table);
  236.   free ((char *) set);
  237. }
  238.  
  239. /* Create a new variable set and push it on the current setlist.  */
  240.  
  241. void
  242. push_new_variable_scope ()
  243. {
  244.   register struct variable_set_list *setlist;
  245.   register struct variable_set *set;
  246.  
  247.   set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
  248.   set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
  249.   set->table = (struct variable **)
  250.     xmalloc (set->buckets * sizeof (struct variable *));
  251.   bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
  252.  
  253.   setlist = (struct variable_set_list *)
  254.     xmalloc (sizeof (struct variable_set_list));
  255.   setlist->set = set;
  256.   setlist->next = current_variable_set_list;
  257.   current_variable_set_list = setlist;
  258. }
  259.  
  260. /* Merge SET1 into SET0, freeing unused storage in SET1.  */
  261.  
  262. static void
  263. merge_variable_sets (set0, set1)
  264.      struct variable_set *set0, *set1;
  265. {
  266.   register unsigned int bucket1;
  267.  
  268.   for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
  269.     {
  270.       register struct variable *v1 = set1->table[bucket1];
  271.       while (v1 != 0)
  272.     {
  273.       struct variable *next = v1->next;
  274.       unsigned int bucket0;
  275.       register struct variable *v0;
  276.  
  277.       if (set1->buckets >= set0->buckets)
  278.         bucket0 = bucket1;
  279.       else
  280.         {
  281.           register char *n;
  282.           bucket0 = 0;
  283.           for (n = v1->name; *n != '\0'; ++n)
  284.         HASH (bucket0, *n);
  285.         }
  286.       bucket0 %= set0->buckets;
  287.  
  288.       for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
  289.         if (streq (v0->name, v1->name))
  290.           break;
  291.  
  292.       if (v0 == 0)
  293.         {
  294.           /* There is no variable in SET0 with the same name.  */
  295.           v1->next = set0->table[bucket0];
  296.           set0->table[bucket0] = v1;
  297.         }
  298.       else
  299.         {
  300.           /* The same variable exists in both sets.
  301.          SET0 takes precedence.  */
  302.           free (v1->value);
  303.           free ((char *) v1);
  304.         }
  305.  
  306.       v1 = next;
  307.     }
  308.     }
  309. }
  310.  
  311. /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1.  */
  312.  
  313. void
  314. merge_variable_set_lists (setlist0, setlist1)
  315.      struct variable_set_list **setlist0, *setlist1;
  316. {
  317.   register struct variable_set_list *list0 = *setlist0;
  318.   struct variable_set_list *last0 = 0;
  319.  
  320.   while (setlist1 != 0 && list0 != 0)
  321.     {
  322.       struct variable_set_list *next = setlist1;
  323.       setlist1 = setlist1->next;
  324.  
  325.       merge_variable_sets (list0->set, next->set);
  326.  
  327.       free ((char *) next);
  328.  
  329.       last0 = list0;
  330.       list0 = list0->next;
  331.     }
  332.  
  333.   if (setlist1 != 0)
  334.     {
  335.       if (last0 == 0)
  336.     *setlist0 = setlist1;
  337.       else
  338.     last0->next = setlist1;
  339.     }
  340. }
  341.  
  342. /* Define the automatic variables, and record the addresses
  343.    of their structures so we can change their values quickly.  */
  344.  
  345. void
  346. define_automatic_variables ()
  347. {
  348.   extern char default_shell[];
  349.   register struct variable *v;
  350.   char buf[200];
  351.  
  352.   sprintf (buf, "%u", makelevel);
  353.   (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
  354.  
  355.   sprintf (buf, "%s%s%s",
  356.        version_string,
  357.        (remote_description == 0 || remote_description[0] == '\0')
  358.        ? "" : "-",
  359.        (remote_description == 0 || remote_description[0] == '\0')
  360.        ? "" : remote_description);
  361.   (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
  362.  
  363.   
  364.   /* This won't override any definition, but it
  365.      will provide one if there isn't one there.  */
  366.   v = define_variable ("SHELL", 5, default_shell, o_default, 0);
  367.   v->export = v_export;        /* Always export SHELL.  */
  368.  
  369. #ifndef __EMX__
  370.   /* Don't let SHELL come from the environment.  */
  371.   if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
  372.     {
  373.       free (v->value);
  374.       v->origin = o_file;
  375.       v->value = savestring (default_shell, strlen (default_shell));
  376.     }
  377. #else
  378.     {
  379.       extern char *os2_shell(int);
  380.       char *cp = os2_shell(0);
  381.       v->value = savestring (cp, strlen(cp));
  382.       for(cp = v->value; *cp != '\0'; cp++)
  383.     *cp = (*cp == '\\' ? '/' : *cp);
  384.     }
  385. #endif
  386.  
  387.   /* Make sure MAKEFILES gets exported if it is set.  */
  388.   v = define_variable ("MAKEFILES", 9, "", o_default, 0);
  389.   v->export = v_ifset;
  390.  
  391.   /* Define the magic D and F variables in terms of
  392.      the automatic variables they are variations of.  */
  393.  
  394.   define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
  395.   define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
  396.   define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
  397.   define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
  398.   define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
  399.   define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
  400.   define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
  401.   define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
  402.   define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
  403.   define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
  404.   define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
  405.   define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
  406. }
  407.  
  408. int export_all_variables;
  409.  
  410. /* Create a new environment for FILE's commands.
  411.    If FILE is nil, this is for the `shell' function.
  412.    The child's MAKELEVEL variable is incremented.  */
  413.  
  414. char **
  415. target_environment (file)
  416.      struct file *file;
  417. {
  418.   struct variable_set_list *set_list;
  419.   register struct variable_set_list *s;
  420.   struct variable_bucket
  421.     {
  422.       struct variable_bucket *next;
  423.       struct variable *variable;
  424.     };
  425.   struct variable_bucket **table;
  426.   unsigned int buckets;
  427.   register unsigned int i;
  428.   register unsigned nvariables;
  429.   char **result;
  430.   unsigned int mklev_hash;
  431.  
  432.   if (file == 0)
  433.     set_list = current_variable_set_list;
  434.   else
  435.     set_list = file->variables;
  436.  
  437.   /* Find the lowest number of buckets in any set in the list.  */
  438.   s = set_list;
  439.   buckets = s->set->buckets;
  440.   for (s = s->next; s != 0; s = s->next)
  441.     if (s->set->buckets < buckets)
  442.       buckets = s->set->buckets;
  443.  
  444.   /* Find the hash value of the bucket `MAKELEVEL' will fall into.  */
  445.   {
  446.     char *p = "MAKELEVEL";
  447.     mklev_hash = 0;
  448.     while (*p != '\0')
  449.       HASH (mklev_hash, *p++);
  450.   }
  451.  
  452.   /* Temporarily allocate a table with that many buckets.  */
  453.   table = (struct variable_bucket **)
  454.     alloca (buckets * sizeof (struct variable_bucket *));
  455.   bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
  456.  
  457.   /* Run through all the variable sets in the list,
  458.      accumulating variables in TABLE.  */
  459.   nvariables = 0;
  460.   for (s = set_list; s != 0; s = s->next)
  461.     {
  462.       register struct variable_set *set = s->set;
  463.       for (i = 0; i < set->buckets; ++i)
  464.     {
  465.       register struct variable *v;
  466.       for (v = set->table[i]; v != 0; v = v->next)
  467.         {
  468.           unsigned int j = i % buckets;
  469.           register struct variable_bucket *ov;
  470.           register char *p = v->name;
  471.  
  472.           if (i == mklev_hash % set->buckets
  473.           && streq (v->name, "MAKELEVEL"))
  474.         /* Don't include MAKELEVEL because it will be
  475.            added specially at the end.  */
  476.         continue;
  477.  
  478.           switch (v->export)
  479.         {
  480.         case v_default:
  481.           if (v->origin == o_default || v->origin == o_automatic)
  482.             /* Only export default variables by explicit request.  */
  483.             continue;
  484.  
  485.           if (! export_all_variables
  486.               && v->origin != o_command
  487.               && v->origin != o_env && v->origin != o_env_override)
  488.             continue;
  489.  
  490.           if (*p != '_' && (*p < 'A' || *p > 'Z')
  491.               && (*p < 'a' || *p > 'z'))
  492.             continue;
  493.           for (++p; *p != '\0'; ++p)
  494.             if (*p != '_' && (*p < 'a' || *p > 'z')
  495.             && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
  496.               break;
  497.           if (*p != '\0')
  498.             continue;
  499.  
  500.         case v_export:
  501.           break;
  502.  
  503.         case v_noexport:
  504.           continue;
  505.  
  506.         case v_ifset:
  507.           if (v->origin == o_default)
  508.             continue;
  509.           break;
  510.         }
  511.  
  512.           for (ov = table[j]; ov != 0; ov = ov->next)
  513.         if (streq (v->name, ov->variable->name))
  514.           break;
  515.           if (ov == 0)
  516.         {
  517.           register struct variable_bucket *entry;
  518.           entry = (struct variable_bucket *)
  519.             alloca (sizeof (struct variable_bucket));
  520.           entry->next = table[j];
  521.           entry->variable = v;
  522.           table[j] = entry;
  523.           ++nvariables;
  524.         }
  525.         }
  526.     }
  527.     }
  528.  
  529.   result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
  530.   nvariables = 0;
  531.   for (i = 0; i < buckets; ++i)
  532.     {
  533.       register struct variable_bucket *b;
  534.       for (b = table[i]; b != 0; b = b->next)
  535.     {
  536.       register struct variable *v = b->variable;
  537.       /* If V is recursively expanded and didn't come from the environment,
  538.          expand its value.  If it came from the environment, it should
  539.          go back into the environment unchanged.  */
  540.       if (v->recursive
  541.           && v->origin != o_env && v->origin != o_env_override)
  542.         {
  543.           char *value = recursively_expand (v);
  544.           result[nvariables++] = concat (v->name, "=", value);
  545.           free (value);
  546.         }
  547.       else
  548.         result[nvariables++] = concat (v->name, "=", v->value);
  549.     }
  550.     }
  551.   result[nvariables] = (char *) xmalloc (100);
  552.   (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
  553.   result[++nvariables] = 0;
  554.  
  555.   return result;
  556. }
  557.  
  558. /* Try to interpret LINE (a null-terminated string) as a variable definition.
  559.  
  560.    ORIGIN may be o_file, o_override, o_env, o_env_override,
  561.    or o_command specifying that the variable definition comes
  562.    from a makefile, an override directive, the environment with
  563.    or without the -e switch, or the command line.
  564.  
  565.    A variable definition has the form "name = value" or "name := value".
  566.    Any whitespace around the "=" or ":=" is removed.  The first form
  567.    defines a variable that is recursively re-evaluated.  The second form
  568.    defines a variable whose value is variable-expanded at the time of
  569.    definition and then is evaluated only once at the time of expansion.
  570.  
  571.    If a variable was defined, a pointer to its `struct variable' is returned.
  572.    If not, NULL is returned.  */
  573.  
  574. struct variable *
  575. try_variable_definition (filename, lineno, line, origin)
  576.      char *filename;
  577.      unsigned int lineno;
  578.      char *line;
  579.      enum variable_origin origin;
  580. {
  581.   register int c;
  582.   register char *p = line;
  583.   register char *beg;
  584.   register char *end;
  585.   enum { bogus, simple, recursive, append } flavor = bogus;
  586.   char *name, *expanded_name, *value;
  587.   struct variable *v;
  588.  
  589.   while (1)
  590.     {
  591.       c = *p++;
  592.       if (c == '\0' || c == '#')
  593.     return 0;
  594.       if (c == '=')
  595.     {
  596.       end = p - 1;
  597.       flavor = recursive;
  598.       break;
  599.     }
  600.       else if (c == ':')
  601.     if (*p == '=')
  602.       {
  603.         end = p++ - 1;
  604.         flavor = simple;
  605.         break;
  606.       }
  607.     else
  608.       /* A colon other than := is a rule line, not a variable defn.  */
  609.       return 0;
  610.       else if (c == '+' && *p == '=')
  611.     {
  612.       end = p++ - 1;
  613.       flavor = append;
  614.       break;
  615.     }
  616.     }
  617.  
  618.   beg = next_token (line);
  619.   while (end > beg && isblank (end[-1]))
  620.     --end;
  621.   p = next_token (p);
  622.  
  623.   /* don't try to do this for empty names */
  624.   if(end < beg)
  625.     return 0;
  626.   /* Expand the name, so "$(foo)bar = baz" works.  */
  627.   name = (char *) alloca (end - beg + 1);
  628.   bcopy (beg, name, end - beg);
  629.   name[end - beg] = '\0';
  630.   expanded_name = allocated_variable_expand (name);
  631.  
  632.   if (expanded_name[0] == '\0')
  633.     {
  634.       if (filename == 0)
  635.     fatal ("empty variable name");
  636.       else
  637.     makefile_fatal (filename, lineno, "empty variable name");
  638.     }
  639.  
  640.   /* Calculate the variable's new value in VALUE.  */
  641.  
  642.   switch (flavor)
  643.     {
  644.     case bogus:
  645.       /* Should not be possible.  */
  646.       abort ();
  647.       return 0;
  648.     case simple:
  649.       /* A simple variable definition "var := value".  Expand the value.  */
  650.       value = variable_expand (p);
  651.       break;
  652.     case recursive:
  653.       /* A recursive variable definition "var = value".
  654.      The value is used verbatim.  */
  655.       value = p;
  656.       break;
  657.     case append:
  658.       /* An appending variable definition "var += value".
  659.      Extract the old value and append the new one.  */
  660.       v = lookup_variable (expanded_name, strlen (expanded_name));
  661.       if (v == 0)
  662.     {
  663.       /* There was no old value.
  664.          This becomes a normal recursive definition.  */
  665.       value = p;
  666.       flavor = recursive;
  667.     }
  668.       else
  669.     {
  670.       /* Paste the old and new values together in VALUE.  */
  671.  
  672.       unsigned int oldlen, newlen;
  673.  
  674.       if (v->recursive)
  675.         /* The previous definition of the variable was recursive.
  676.            The new value comes from the unexpanded old and new values.  */
  677.         flavor = recursive;
  678.       else
  679.         /* The previous definition of the variable was simple.
  680.            The new value comes from the old value, which was expanded
  681.            when it was set; and from the expanded new value.  */
  682.         p = variable_expand (p);
  683.  
  684.       oldlen = strlen (v->value);
  685.       newlen = strlen (p);
  686.       value = (char *) alloca (oldlen + 1 + newlen + 1);
  687.       bcopy (v->value, value, oldlen);
  688.       value[oldlen] = ' ';
  689.       bcopy (p, &value[oldlen + 1], newlen + 1);
  690.     }
  691.     }
  692.  
  693.   v = define_variable (expanded_name, strlen (expanded_name),
  694.                value, origin, flavor == recursive);
  695.  
  696.   free (expanded_name);
  697.  
  698.   return v;
  699. }
  700.  
  701. /* Print information for variable V, prefixing it with PREFIX.  */
  702.  
  703. static void
  704. print_variable (v, prefix)
  705.      register struct variable *v;
  706.      char *prefix;
  707. {
  708.   char *origin;
  709.  
  710.   switch (v->origin)
  711.     {
  712.     case o_default:
  713.       origin = "default";
  714.       break;
  715.     case o_env:
  716.       origin = "environment";
  717.       break;
  718.     case o_file:
  719.       origin = "makefile";
  720.       break;
  721.     case o_env_override:
  722.       origin = "environment under -e";
  723.       break;
  724.     case o_command:
  725.       origin = "command line";
  726.       break;
  727.     case o_override:
  728.       origin = "`override' directive";
  729.       break;
  730.     case o_automatic:
  731.       origin = "automatic";
  732.       break;
  733.     case o_invalid:
  734.     default:
  735.       abort ();
  736.       break;
  737.     }
  738.   printf ("# %s\n", origin);
  739.  
  740.   fputs (prefix, stdout);
  741.  
  742.   /* Is this a `define'?  */
  743.   if (v->recursive && index (v->value, '\n') != 0)
  744.     printf ("define %s\n%s\nendef\n", v->name, v->value);
  745.   else
  746.     {
  747.       register char *p;
  748.  
  749.       printf ("%s %s= ", v->name, v->recursive ? "" : ":");
  750.  
  751.       /* Check if the value is just whitespace.  */
  752.       p = next_token (v->value);
  753.       if (p != v->value && *p == '\0')
  754.     /* All whitespace.  */
  755.     printf ("$(subst ,,%s)", v->value);
  756.       else if (v->recursive)
  757.     fputs (v->value, stdout);
  758.       else
  759.     /* Double up dollar signs.  */
  760.     for (p = v->value; *p != '\0'; ++p)
  761.       {
  762.         if (*p == '$')
  763.           putchar ('$');
  764.         putchar (*p);
  765.       }
  766.       putchar ('\n');
  767.     }
  768. }
  769.  
  770.  
  771. /* Print all the variables in SET.  PREFIX is printed before
  772.    the actual variable definitions (everything else is comments).  */
  773.  
  774. static void
  775. print_variable_set (set, prefix)
  776.      register struct variable_set *set;
  777.      char *prefix;
  778. {
  779.   register unsigned int i, nvariables, per_bucket;
  780.   register struct variable *v;
  781.  
  782.   per_bucket = nvariables = 0;
  783.   for (i = 0; i < set->buckets; ++i)
  784.     {
  785.       register unsigned int this_bucket = 0;
  786.  
  787.       for (v = set->table[i]; v != 0; v = v->next)
  788.     {
  789.       ++this_bucket;
  790.       print_variable (v, prefix);
  791.     }
  792.  
  793.       nvariables += this_bucket;
  794.       if (this_bucket > per_bucket)
  795.     per_bucket = this_bucket;
  796.     }
  797.  
  798.   if (nvariables == 0)
  799.     puts ("# No variables.");
  800.   else
  801.     {
  802.       printf ("# %u variables in %u hash buckets.\n",
  803.           nvariables, set->buckets);
  804. #ifndef    NO_FLOAT
  805.       printf ("# average of %.1f variables per bucket, \
  806. max %u in one bucket.\n",
  807.           (double) nvariables / (double) set->buckets,
  808.           per_bucket);
  809. #endif
  810.     }
  811. }
  812.  
  813.  
  814. /* Print the data base of variables.  */
  815.  
  816. void
  817. print_variable_data_base ()
  818. {
  819.   puts ("\n# Variables\n");
  820.  
  821.   print_variable_set (&global_variable_set, "");
  822. }
  823.  
  824.  
  825. /* Print all the local variables of FILE.  */
  826.  
  827. void
  828. print_file_variables (file)
  829.      struct file *file;
  830. {
  831.   if (file->variables != 0)
  832.     print_variable_set (file->variables->set, "# ");
  833. }
  834.